﻿using System.Text.RegularExpressions;
using DynamicControllersGeneration;

namespace ClientOverflow.ViewModels.Register
{
    /// <summary>
    /// RegexValidator is an implementation of IValidator. This validator checks if some string match the
    /// expected regular expression pattern. The regex validator is passed to the dynamic controllers generation
    /// framework which calls the validator from the dynamic proxy.
    /// </summary>
    internal class RegexValidator : IValidator
    {
        private readonly string pattern;
        private readonly string errorMessage;

        public RegexValidator(string pattern, string errorMessage)
        {
            this.pattern = pattern;
            this.errorMessage = errorMessage;
        }

        public string Validate(object value)
        {
            value = value ?? string.Empty;
            if (!Regex.IsMatch((string) value, pattern))
            {
                return errorMessage;
            }

            return null;
        }
    }
}